package com.smart.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.smart.dao.NoticeDAO;
import com.smart.dao.NoticeTemplateDAO;
import com.smart.model.Notice;
import com.smart.model.NoticeTemplate;

/**
 * Servlet implementation class NoticeServlet
 */
@Controller
public class NoticeServlet extends HttpServlet {
	private static String uploadPath=null;
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticeServlet() {
        super();
    }

	@RequestMapping(value="/pc/add_notice", method = RequestMethod.POST)
    public void addNotice(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
		System.out.println("NOTICE!");
		Enumeration<String> en=req.getParameterNames();
		String title = req.getParameter("title");
		String summary = req.getParameter("summary");
		String content = req.getParameter("content[]");
		Notice notice = new Notice();
		//System.out.println("title:"+title+"\ncontent:"+content);
		notice.setTitle(title);
		notice.setSummary(summary);
		notice.setContent(content);
		NoticeDAO noticeDAO = new NoticeDAO();
		PrintWriter out = resp.getWriter();
		try {
			System.out.println("ready to ...");
			noticeDAO.addNotice(notice);
			System.out.println("finish");
			out.print(true);
		} catch (SQLException e) {
			out.print(false);
			e.printStackTrace();
		}
		
      //return "redirect:/views/pc/send-community.html";
    }

    @RequestMapping(value="/wechat/get_notices_list", method = RequestMethod.POST)
    public void getNoticeList(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException
    {
    	System.out.println("get_notices_list");
    	NoticeDAO noticeDAO = new NoticeDAO();
    	String infoId = req.getParameter("infoid");
    	int id = Integer.MAX_VALUE;
    	System.out.println("infoId:"+infoId);
    	if(infoId != null){
    		id =Integer.parseInt(infoId);
    	}
    	List <Notice> noticeList = noticeDAO.listNotice(id);
    	JSONArray noticeArray = new JSONArray();
    	for(Notice ntc : noticeList)
    	{
    		System.out.println("content:"+ntc.getContent());
    		JSONObject jsonObject = JSONObject.fromObject(ntc);
    		noticeArray.add(jsonObject);
    	}

    	String notices = JSONArray.fromObject(noticeArray).toString();
    	System.out.println(notices);
    	resp.setCharacterEncoding("UTF-8");
    	PrintWriter out = resp.getWriter();
    	out.print(notices);
    }
	
    @RequestMapping(value="/wechat/get_notice", method = RequestMethod.POST)
    public void getNotice(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException
    {
    	NoticeDAO noticeDAO = new NoticeDAO();
    	System.out.println("infoid="+req.getParameter("infoid"));
    	Notice ntc = noticeDAO.findNotice(Integer.parseInt(req.getParameter("infoid")));
    	JSONObject jsonObject = JSONObject.fromObject(ntc);
    	String notice = jsonObject.toString();
    	System.out.println(notice);
    	resp.setCharacterEncoding("UTF-8");
    	PrintWriter out = resp.getWriter();
    	out.print(notice);
    }
    
    @RequestMapping(value="/pc/add_template", method = RequestMethod.POST)
    public void saveTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    	NoticeTemplateDAO tmpDAO = new NoticeTemplateDAO();
    	String name = req.getParameter("name");
    	String title = req.getParameter("title");
    	String content = req.getParameter("content[]");
    	NoticeTemplate template = new NoticeTemplate();
    	template.setName(name);
    	template.setTitle(title);
    	template.setContent(content);
    	PrintWriter out = resp.getWriter();
    	try {
			tmpDAO.saveTemplate(template);
			out.print(true);
		} catch (SQLException e) {
			out.print(false);
			e.printStackTrace();
		}
    }
    
    @RequestMapping(value="/pc/get_template_list", method = RequestMethod.POST)
    public void getTmpList(HttpServletRequest req, HttpServletResponse resp) {
    	System.out.println("getTmpList method...");
    	NoticeTemplateDAO tmpDAO = new NoticeTemplateDAO();
    	List<NoticeTemplate> list = new ArrayList<NoticeTemplate>();
    	try {
			list = tmpDAO.getTemplateList();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    	JSONArray noticeArray = new JSONArray();
    	for(NoticeTemplate tmp : list)
    	{
    		
    		JSONObject jsonObject = JSONObject.fromObject(tmp);
    		noticeArray.add(jsonObject);
    	}

    	String result = JSONArray.fromObject(noticeArray).toString();
    	System.out.println("template list:"+result);
    	try {
    		PrintWriter out = resp.getWriter();
			out.print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    @RequestMapping(value="/pc/get_template", method = RequestMethod.POST)
    public void getTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    	NoticeTemplateDAO tmpDAO = new NoticeTemplateDAO();
    	int id = Integer.parseInt(req.getParameter("id"));
    	System.out.println("get_template parameter:"+ id);
    	NoticeTemplate template = new NoticeTemplate();
    	try {
			template = tmpDAO.getTemplateById(id);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    	JSONObject jsonObject = JSONObject.fromObject(template);
    	PrintWriter out = resp.getWriter();
		out.print(jsonObject);
    }
    
    public static void main(String[] args)
    {

    }
}
